﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace RelaxUI
{
    public partial class ResourceManager
    {
        

        /// <summary>
        /// 需要向页面注册的样式列表
        /// </summary>
        private Dictionary<string, string> _startupCssDic = new Dictionary<string, string>();

        private Page _page = null;
        public Page Page
        {
            get
            {
                return _page;
            }
        }
        public ResourceManager()
        {
            _page = HttpContext.Current.Handler as Page;
            _page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
        }


        #region SetupFirstLoadResource

        /// <summary>
        /// 注册页面第一次加载时的样式和脚本
        /// </summary>
        private void SetupFirstLoadResource()
        {
            // 页头注册公共CSS/Javascript
            //CommonResourceHelper.RegisterCommonResource(Page);

        }


        private void RegisterStartupCss()
        {
            //if (_startupCssDic.Count > 0)
            //{
            //    StringBuilder sb = new StringBuilder();
            //    foreach (string value in _startupCssDic.Values)
            //    {
            //        sb.Append(value);
            //    }
            //    CommonResourceHelper.RegisterHeaderCSS(Page, sb.ToString());
            //}

        }

        /// <summary>
        /// 注册页面脚本
        /// </summary>
        public void RegisterStartupScript()
        {
            
        }

        #endregion

        #region Page_PreRenderComplete

        /// <summary>
        /// 准备呈现页面内容，在保存页面状态之前
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            // 如果是 FineUI 的Ajax
            if (IsFineUIAjaxPostBack)
            {
                // 注意：这里不能设置 text/html，因为有上传控件时，会把返回的内容放在IFRAME中模拟Ajax过程。
                //HttpContext.Current.Response.ContentType = "text/plain";
                HttpContext.Current.Trace.IsEnabled = false;
                HttpContext.Current.Response.ContentType = "text/plain";
                //HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                //HttpContext.Current.Response.Charset = "UTF-8";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                

                RelaxUI.ResponseFilter filter = new RelaxUI.ResponseFilter(HttpContext.Current.Response.Filter);
                HttpContext.Current.Response.Filter = filter;
            }
            else
            {
                SetupFirstLoadResource();
            }
        }

        #endregion

        #region IsFineUIAjaxPostBack

        public bool IsFineUIAjaxPostBack
        {
            get
            {
                return HttpContext.Current.Request.Form["X_AJAX"] == "true";
            }
        }

        #endregion

    }
}
